unit uSaveComeralState;

interface

uses
  System.Classes, uBaseCompThread, System.SysUtils, Vcl.ComCtrls;

type
  TSaveComeralState = class(TBaseCompThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  end;

var
  SaveComeralState: TSaveComeralState;

implementation

uses
  uCompDM;

{ TSaveComeralState }

procedure TSaveComeralState.Execute;
begin
  with CompDM do
  begin
    Self.Enabled := False;
    Synchronize(ChangeControlAvailability);

    try
      // Удаление старой информации из таблицы
      try
        DelTmpComeralInvQ.ExecSQL;
      except
        on E: Exception do
          raise EDelTmpComeralInvException('Ошибка удаление информации из таблицы tmpComeralInv, с текстом: '
            + e.Message)
      end;

      // Сохранение текущей сверки в таблицу tmpComeralInv
      Synchronize(CompareMemTableEh.DisableControls);
      CompareMemTableEh.First;

      Self.PBStyle := pbstMarquee;
      Synchronize(SetProgressBarStyle);

      while not CompareMemTableEh.Eof do
      begin
        try
          InsertTmpComeralInv(CompareMemTableEhidPredpr.AsVariant,
            CompareMemTableEhinvoicefullname.AsVariant, CompareMemTableEhdatetrans.AsVariant,
            CompareMemTableEhstatus.AsVariant, CompareMemTableEhrecipientname.AsVariant,
            CompareMemTableEhsignerfio.AsVariant, CompareMemTableEhnamepredpr.AsVariant,
            CompareMemTableEhdocnumber.AsVariant, CompareMemTableEhdocdate.AsVariant,
            CompareMemTableEhndsexcel.AsVariant, CompareMemTableEhndsspr.AsVariant,
            CompareMemTableEhndsinvoice.AsVariant, CompareMemTableEhtotalcostinv.AsVariant,
            CompareMemTableEhtotalcostvatinv.AsVariant, CompareMemTableEhfilial.AsVariant,
            CompareMemTableEhsvplace.AsVariant, CompareMemTableEhnumbuhspr.AsVariant,
            CompareMemTableEhnumdocpart.AsVariant);
        except
          on e: EInsTmpComeralInvException do
          begin
            Self.DialogMessage := 'Произошла ошибка с текстом: ' + e.Message;
            Synchronize(ShowErrorDialog);
          end;
        end;

        CompareMemTableEh.Next;
      end;

      Synchronize(CompareMemTableEh.EnableControls);

      // Сохранение текущих проводок в таблицу tmpComeralProv
      try
        RewriteInsTmpComeralProv.ExecSQL;
      except
        on e: Exception do
        begin
          Self.DialogMessage := 'Ошибка перезаписи проводок с текстом: ' + e.Message;
          Synchronize(ShowErrorDialog);
        end;
      end;
    except
      on edel: EDelTmpComeralInvException do
      begin
        Self.DialogMessage := edel.Message;
        Synchronize(ShowErrorDialog);
      end;
      on E: Exception do
      begin
        Self.DialogMessage := 'Ошибка сохранения текущей сверки, с текстом: ' + e.Message;
        Synchronize(ShowErrorDialog);
      end;
    end;

    Self.PBStyle := pbstNormal;
    Synchronize(SetProgressBarStyle);

    Self.Enabled := True;
    Synchronize(ChangeControlAvailability);

    Self.DialogMessage := 'Сохранение завершено';
    Synchronize(ShowInfoDialog)
  end;
end;

end.

